import React from 'react'
import { connect } from 'dva'
import { Form ,Modal,message} from 'antd'
import { hashHistory } from 'dva/router'
import { mapStateToProps } from 'utils/view_utils'
import styles from './competingGoodsListStyle.less'
import ListPage from 'widget/PageTmpl/ListPageQiance'
import classnames from 'classnames/bind';
import FlagBtn from 'widget/FlagBtn/FlagView'
import config from 'config/config'
const confirm = Modal.confirm;
let cx=classnames.bind(styles);


const competingGoodsListView = function({dispatch, model, form}) {
  // 表单的子函数
  // const {resetFields, getFieldDecorator, validateFields, setFieldsValue} = form
  // 取出model对象，命名空间
  const { modelObj, namespace } = model
  // modelObj.xxx获取model中状态机的值
  let {biz,areaList,products} = modelObj

  let pageProps = {
    // ---------- 界面部分 ----------
    ui: {
      // 页面接口地址
      api_url: `${config.aheadPlan}admin/competingGoods/competingGoods/list`,
      // 接口方式
      method: "GET",
      // "data_field_name":"data",
      app: "",
      // 接口参数
      params: {
        pageSize:10,
        pageNo:1,
        },
        
      // 模拟数据 - 用于开发需要，若填充了上面的api_url之后，请将此项设置为null
      // "mockData": mockData,
      mockData: null,
      // 查询栏
      search_bar: {
        fields: [
          {
            en_name: 'areaId',
            zh_name: '区域',
            elem_type: 'SelectFilter',
            elem_valid_type: 'string',
            showSearch: true,
            cmpt_items: areaList,
          },
          {
            en_name: 'relationProject',
            zh_name: '对标我司项目',
            elem_type: 'SelectFilter',
            elem_valid_type: 'string',
            showSearch: true,
            cmpt_items: products,
            onFocus:()=>{
              if(biz.queryForm && biz.queryForm.areaId){ 
                sessionStorage.setItem('competingGoodsListId',biz.queryForm.areaId)
                let areaId= biz.queryForm.areaId
                dispatch({
                  type: `${namespace}/filterProjectList`,
                  payload: {areaId}
                })
              }
            },
          },
          {
            en_name: 'competingGoodsName',
            zh_name: '竞品项目',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'competingGoodsType',
            zh_name: '竞品类型',
            elem_type: 'SelectFilter',
            elem_valid_type: 'string',
            showSearch: true,
            cmpt_items: [
              {
                value: '一级竞品',
                label: '一级竞品'
              }, 
              {
                value: '二级竞品',
                label: '二级竞品'
              },
              {
                value: '三级竞品',
                label: '三级竞品'
              }
            ],
          },
          {
            en_name: 'isSale',
            zh_name: '竞品状态',
            elem_type: 'SelectFilter',
            elem_valid_type: 'string',
            showSearch: true,
            cmpt_items: [
              {
                value: '在售',
                label: '在售'
              },
              {
                value: '未售',
                label: '未售'
              }
            ],
          },
          {
            en_name: 'address',
            zh_name: '地址',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
        ],
        searchHandler: () => { // 自定义搜索按钮回调函数，点击搜索时触发
          console.log('search click')
        },
        resetHandler: () => { // 自定义重置按钮回调函数，点击重置时触发
          console.log('reset click')
        },
        searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
          console.log('search finish', params)
        },
        pasteTargetList: [  // 列表组件中将输入框的en_name作为默认id(也可以通过id另外设置)
          'channel',  // 将需要粘贴事件拦截的输入框id以数组的形式传入, 该参数对应PasteFromClipboard组件的targetList参数
        ]
      },
      // 数据表格  序号 是否生效 项目名称  佣金方案名称 方案生效日期 方案失效日期 审批人 审批通过日期   提交人 提交日期  创建人 创建日期
      table:
        {
          status_params: "status=0",
          status_text: "全部",
          // 表头字段列表
          scroll: { x: 1800},
          fields: [
            {
              en_name: "ruleNo",
              zh_name: "序号",
              width: 60,
              render: (a, item, index, d, e, f) => {
                return index+1
              },
            },
            {
                en_name: "areaName",
                zh_name: "区域",
            },
            {
                en_name: "competingGoodsName",
                zh_name: "竞品项目",
            },
            {
                en_name: "relationProject",
                zh_name: "对标我司项目",
            },
            {
                en_name: "address",
                zh_name: "地址",
            },
            {
                en_name: "competingGoodsType",
                zh_name: "竞品类型",
            },
            {
              en_name: "isSale",
              zh_name: "竞品状态",
              render:(a, item, index, d, e, f)=>{
                return item.isSale
              }
            },
            {
                en_name: "floorSpace",
                zh_name: "总占地(m²)",
            },
            {
                en_name: "plotRatio",
                zh_name: "容积率",
            },
            {
              en_name: "edit",
              zh_name: "操作",
              width:130,
              fixed:'right',
              render: (a, item, index, d, e, f) => {
                return (
                    <span>
                        <FlagBtn node={<a onClick={() => addOrEditSkip('edit',item)}>编辑<span className="ant-divider" /></a>} btnId={''}> </FlagBtn>
                        <FlagBtn node={<a onClick={() => deleteFun(item)}>删除</a>} btnId={''}> </FlagBtn>
                    </span>
                )
              },
            }
          ]
        },
      // 操作栏
      action_bar: [
        {
          label: "新增",
          type: "primary",
          onClick: (e,row) =>{
            hashHistory.push({
              pathname: '/home/predictManage/competingGoodsDetail',
              query: {}
            })
          }
        }
      ]
    },
  }

  // 查看及编辑跳转
  function addOrEditSkip(type,item){
      hashHistory.push({
        pathname: '/home/predictManage/competingGoodsDetail',
        query: {actionType: type, id:item.id }
      })
  }
//删除
function deleteFun(item){
    confirm({
    title: '删除确认',
    content: '是否删除该竞品信息!',
    okText: '确认',
    okType: 'primary',
    cancelText: '取消',
    onOk() {
        dispatch({
            type: `${namespace}/deleteFun`,
            payload: { id: item.id}
        })
    },
    onCancel() {
        console.log('Cancel');
    },
    });

}

  if (!!modelObj.biz) {
		pageProps.biz = modelObj.biz
		pageProps.biz.syncBackCbf = (biz) => {
			dispatch({
				type: `${namespace}/updateStore`,
				payload: { biz }
			})
		}
  }

  return (
    <div className={cx('pageLayout')}>
      <ListPage pageProps={pageProps}/>
    </div>
  )
}
// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(competingGoodsListView))
